package com.ruoyi.web.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送短信工具类
 */
public class SendMsgUtil {
    /**
     * Demo
     */
    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
/*        map.put("Status","预警");
        map.put("Time","2020-09-08");
        map.put("Equipment","大金设备测试");
        map.put("Parts","传感器");
        map.put("Content","温度100C");
        SendMsgUtil.sendAlarm(map,"19176483846");*/

        map.put("Start_Time","2020-09-08");
        map.put("End_Time","2020-09-08");
        map.put("Equipment","大金设备测试");
        map.put("Parts","刀头");
        map.put("Type","普通保养");
        map.put("Content","清洗刀头");
        SendMsgUtil.sendMaintain(map,"19176483846");
    }

    /**
     * 发送告警记录
     * @param param
     * @param phone maintain
     * @return
     */
    public static boolean sendAlarm(Map<String, Object> param, String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "LTAI5tSfufUZ6eWYwWKHysoW", "IRPMcwp8RYInjDHE7IVq6oxAsw9E02");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "蓝亚信息技术");
        request.putQueryParameter("TemplateCode", "SMS_222336416");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
             return false;
        }
        return true;
    }

    /**
     * 发送保养记录
     * @param param
     * @param phone
     * @return
     */
    public static boolean sendMaintain(Map<String, Object> param, String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "LTAI5tSfufUZ6eWYwWKHysoW", "IRPMcwp8RYInjDHE7IVq6oxAsw9E02");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "蓝亚信息技术");
        request.putQueryParameter("TemplateCode", "SMS_222336415");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            return false;
        }
        return true;
    }

}
